<?php

class UserTag extends PersistentObject
{
   const TABLE = "cms_user_tags";
   
   const COLOR_TRNSPRNT = "transparent";
   const COLOR_RED    = "#FF0000";
   const COLOR_BLUE   = "#0000FF";
   const COLOR_GREEN  = "#00FF00";
   const COLOR_YELLOW = "#FFFF00";
   const COLOR_ORANGE = "#FFCC00";
   const COLOR_GREY   = "#999999";
   
   // COlores de google
   const COLOR_GRED     = "#FB4C2F";
   const COLOR_GORANGE  = "#FFAD47";
   const COLOR_GYELLOW  = "#FAD165";
   const COLOR_GGREEN   = "#16A766";
   const COLOR_GLGHTGRN = "#43D692";
   const COLOR_GBLUE    = "#4A86E8";
   const COLOR_GVIOLET  = "#A479E2";
   const COLOR_GPINK    = "#F691B3";
   
   /*
   // Tonos rojos
   #F6C5BE
   #EFA093
   #E66550
   #CC3A21
   #AC2B16
   #822111
   
   // Tonos azules
   #C9DAF8
   #6D9EEB
   #285BAC
   */
   
   static function getColorList()
   {
      return array(
        self::COLOR_TRNSPRNT,
        self::COLOR_RED,
        self::COLOR_BLUE,
        self::COLOR_GREEN,
        self::COLOR_YELLOW,
        self::COLOR_ORANGE,
        self::COLOR_GREY,
        
        self::COLOR_GRED,
        self::COLOR_GORANGE,
        self::COLOR_GYELLOW,
        self::COLOR_GGREEN,
        self::COLOR_GLGHTGRN,
        self::COLOR_GBLUE,
        self::COLOR_GVIOLET,
        self::COLOR_GPINK
      );
   }
   static function getColorMap()
   {
      return array(
        self::COLOR_TRNSPRNT => self::COLOR_TRNSPRNT,
        self::COLOR_RED      => self::COLOR_RED,
        self::COLOR_BLUE     => self::COLOR_BLUE,
        self::COLOR_GREEN    => self::COLOR_GREEN,
        self::COLOR_YELLOW   => self::COLOR_YELLOW,
        self::COLOR_ORANGE   => self::COLOR_ORANGE,
        self::COLOR_GREY     => self::COLOR_GREY,
        
        self::COLOR_GRED     => self::COLOR_GRED,
        self::COLOR_GORANGE  => self::COLOR_GORANGE,
        self::COLOR_GYELLOW  => self::COLOR_GYELLOW,
        self::COLOR_GGREEN   => self::COLOR_GGREEN,
        self::COLOR_GLGHTGRN => self::COLOR_GLGHTGRN,
        self::COLOR_GBLUE    => self::COLOR_GBLUE,
        self::COLOR_GVIOLET  => self::COLOR_GVIOLET,
        self::COLOR_GPINK    => self::COLOR_GPINK
      );
   }
   
   // Para crear un mapping que pueda ser usado como options de un select.
   // TODO: esto puede ir dentro del framework, es bien generico.
   // $exclude es un array de objetos de esta misma clase.
   static function selectTags($k, $v, $exclude = array())
   {
      $tags = self::listAll(new ArrayObject());
     
      // llama a una funcion anonima sobre cada objeto de $exclude
      // http://stackoverflow.com/questions/6840485/call-methods-of-objects-in-array-using-array-map
      // array_fill crea un array dfe params con $k, es la unica forma de pasarle $k a la funcion anonima
      $excludeKeys = array_map( function($obj, $k) { return $obj->aGet($k); }, $exclude, array_fill(0, sizeof($exclude), $k));
      
      //print_r($excludeKeys);
      
      $options = array();
      foreach ($tags as $tag)
      {
         $optk = $tag->aGet($k);
         if (!in_array($optk, $excludeKeys))
         {
            $options[ $optk ] = $tag->aGet($v);
         }
      }
      
      return $options;
   }
   
   function __construct($args = array (), $isSimpleInstance = false)
   {
      $this->setWithTable(self::TABLE);
        
      $this->addAttribute("name",         Datatypes :: TEXT);
      $this->addAttribute("description",  Datatypes :: TEXT);
      $this->addAttribute("color",        Datatypes :: TEXT);
      $this->addAttribute("image",        Datatypes :: TEXT); // URL a una imagen
      $this->addAttribute("points",       Datatypes :: INT_NUMBER );

      $this->addHasMany('subtags', 'UserTag', self::HASMANY_SET);
      
      /*
      $this->setCreatedOn(date("Y-m-d H:i:s"));
      $this->setShowContainer(true);
      $this->setShowInAllPages(false);
      $this->setStatus(self::STATUS_NORMAL);
      */
      
      $this->addConstraints("color",  array( Constraint::maxLength(15), Constraint::inList(self::getColorList()) ));
      $this->addConstraints("image",  array( Constraint::nullable(true) ));
      $this->addConstraints("description",  array( Constraint::nullable(true) ));
      $this->addConstraints("points", array( Constraint::between(0, 100) ));
      

      parent :: __construct($args, $isSimpleInstance);
   }

   public static function listAll(ArrayObject $params)
   {
      self :: $thisClass = __CLASS__;
      return PersistentObject :: listAll($params);
   }

   public static function count()
   {
      self :: $thisClass = __CLASS__;
      return PersistentObject :: count();
   }

   public static function get($id)
   {
      self :: $thisClass = __CLASS__;
      return PersistentObject :: get($id);
   }

   public static function findBy(Condition $condition, ArrayObject $params)
   {
      self :: $thisClass = __CLASS__;
      return PersistentObject :: findBy($condition, $params);
   }

   public static function countBy(Condition $condition)
   {
      self :: $thisClass = __CLASS__;
      return PersistentObject :: countBy($condition);
   }
}
?>